package com.lwc.cfdns.controller.admin.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.bo.system.RoleBO;
import com.lwc.cfdns.pojo.dto.ChangeStatusDTO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.RoleAuthUserDTO;
import com.lwc.cfdns.pojo.dto.system.RoleDTO;
import com.lwc.cfdns.pojo.dto.system.RoleDataScopeDTO;
import com.lwc.cfdns.pojo.entity.SystemAdmin;
import com.lwc.cfdns.pojo.entity.SystemRole;
import com.lwc.cfdns.pojo.vo.system.RoleVO;
import com.lwc.cfdns.service.impl.SystemRoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 角色
 *
 * @author luwc
 * @title RoleController
 * @description
 * @time 2023/5/17 16:31:51
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class RoleController {
    @Autowired
    private SystemRoleServiceImpl roleService;

    /**
     * 创建角色
     *
     * @param roleDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/18 09:36:07
     */
    @PreAuthorize("hasAnyAuthority('system:role:create')")
    @PostMapping("/role")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody RoleDTO roleDTO) {
        return roleService.create(roleDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return RoleVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/18 10:54:00
     */
    @PreAuthorize("hasAnyAuthority('system:role:view')")
    @GetMapping("/role/{id}")
    public RoleVO view(@PathVariable("id") @NotEmpty(message = "ID不能为空") Long id) {
        return roleService.view(id);
    }

    /**
     * 更新
     *
     * @param id
     * @param roleDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/18 11:08:23
     */
    @PreAuthorize("hasAnyAuthority('system:role:update')")
    @PutMapping("/role/{id}")
    public Map<String, Object> update(@PathVariable("id") @NotEmpty(message = "ID不能为空") Long id, @Validated(ValidGroup.Update.class) @RequestBody RoleDTO roleDTO) {
        return roleService.update(id, roleDTO);
    }

    /**
     * 删除
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/18 15:30:57
     */
    @PreAuthorize("hasAnyAuthority('system:role:delete')")
    @DeleteMapping("/role/{ids}")
    public Map<String, Object> delete(@PathVariable("ids") @NotBlank(message = "ID不能为空") String ids) {
        return roleService.delete(ids);
    }

    /**
     * 列表
     *
     * @param roleBO
     * @return IPage<SystemRole>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/18 15:40:29
     */
    @PreAuthorize("hasAnyAuthority('system:role:lists')")
    @GetMapping("/role")
    public IPage<SystemRole> lists(RoleBO roleBO) {
        return roleService.lists(roleBO);
    }

    /**
     * 树
     *
     * @param roleBO
     * @return List<RoleVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/18 16:41:48
     */
    @GetMapping("/role/tree")
    public List<Map<String, Object>> tree(RoleBO roleBO) {
        return roleService.tree(roleBO);
    }

    /**
     * 状态修改
     *
     * @param changeStatusDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 15:59
     */
    @PutMapping("/role/changeStatus")
    public Map<String, Object> changeStatus(@RequestBody ChangeStatusDTO changeStatusDTO) {
        return roleService.changeStatus(changeStatusDTO);
    }

    /**
     * 数据权限
     *
     * @param roleDataScopeDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 10:15:39
     */
    @PreAuthorize("hasAnyAuthority('system:role:dataScope')")
    @PutMapping("/role/dataScope")
    public Map<String, Object> dataScope(@RequestBody RoleDataScopeDTO roleDataScopeDTO) {
        return roleService.dataScope(roleDataScopeDTO);
    }

    /**
     * 批量给角色添加授权用户
     *
     * @param roleAuthUserDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 13:48:54
     */
    @PutMapping("/role/addAuthUser")
    public Map<String, Object> addAuthUser(@RequestBody RoleAuthUserDTO roleAuthUserDTO) {
        return roleService.addAuthUser(roleAuthUserDTO);
    }

    /**
     * 批量给角色取消授权用户
     *
     * @param roleAuthUserDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/30 13:48:54
     */
    @PutMapping("/role/cancelAuthUser")
    public Map<String, Object> cancelAuthUser(@RequestBody RoleAuthUserDTO roleAuthUserDTO) {
        return roleService.cancelAuthUser(roleAuthUserDTO);
    }
}
